<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryFormRef" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="年份" prop="contractYear">
          <el-date-picker 
            v-model="queryParams.contractYear" 
            type="year" 
            placeholder="选择年" 
            format="YYYY年"
            value-format="YYYY">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="供应商" prop="supplierId">
          <el-select 
            v-model="queryParams.supplierId" 
            placeholder="请选择供应商" 
            clearable 
            filterable>
            <el-option 
              v-for="dict in sys_supplier_query" 
              :key="dict.value" 
              :label="dict.label"
              :value="dict.value" 
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
          <el-button :icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="季度统计" name="first">
          <el-table v-loading="loading" :data="quarterList">
            <el-table-column label="季度" align="center" prop="contractQuarter" />
            <el-table-column label="供应商名称" align="center" prop="supplierName" />
            <el-table-column label="应付金额" align="center" prop="contractAmount" />
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="月份统计" name="second">
          <el-table v-loading="loading" :data="monthList">
            <el-table-column label="月份" align="center" prop="contractMonth" />
            <el-table-column label="供应商名称" align="center" prop="supplierName" />
            <el-table-column label="应付金额" align="center" prop="contractAmount" />
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </div>
  </template>
  
  <script setup name="SupplierBookReport">
  import { ref, reactive, onMounted } from 'vue'
  import { getCurrentInstance } from 'vue'
  import { ElMessage, ElMessageBox } from 'element-plus'
  import { Search, Refresh } from '@element-plus/icons-vue'
  import { monthSupplierReport, quarterSupplierReport } from "@/api/purchaseReport/purchaseContractReport"
  
  const { proxy } = getCurrentInstance()
  const { sys_supplier_query } = proxy.useDict("sys_supplier_query")
  
  const activeName = ref('first')
  const currentYear = ref(new Date().getFullYear().toString())
  // 遮罩层
  const loading = ref(true)
  // 显示搜索条件
  const showSearch = ref(true)
  // 采购合同报表表格数据
  const monthList = ref([])
  const quarterList = ref([])
  
  // 查询参数
  const queryParams = reactive({
    contractNo: null,
    supplierId: null,
    supplierName: null,
    contractYear: currentYear.value,
    contractQuarter: null,
    contractMonth: null
  })
  
  const queryFormRef = ref()
  
  /** 查询采购合同报表列表 */
  const getList = async () => {
    loading.value = true
    try {
      const [monthResponse, quarterResponse] = await Promise.all([
        monthSupplierReport(queryParams),
        quarterSupplierReport(queryParams)
      ])
      monthList.value = monthResponse.rows
      quarterList.value = quarterResponse.rows
    } catch (error) {
      console.error('获取报表数据失败:', error)
    } finally {
      loading.value = false
    }
  }
  
  /** 搜索按钮操作 */
  const handleQuery = () => {
    getList()
  }
  
  /** 重置按钮操作 */
  const resetQuery = () => {
    proxy?.resetForm("queryFormRef")
    queryParams.contractYear = currentYear.value
    handleQuery()
  }
  
  onMounted(() => {
    getList()
  })
  </script> 